package cn.fr4nk.crawler.handler;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class TestRegShareholder {
	
	private static Log log = LogFactory.getLog(TestRegShareholder.class);

	public static void main(String[] args) {
		String wholePage = loadHtml("c:/tmp/detail.html");
		getShareholderType(wholePage);
	}
	
	public static String loadHtml(String filePath) {
		BufferedReader reader = null;
		StringBuilder sb = new StringBuilder();
		try {
			reader = new BufferedReader(new FileReader(filePath));
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line+'\n');
			}
		} catch (FileNotFoundException e) {
			log.warn("读取html失败，文件不存在");
		} catch (IOException e) {
			log.warn("读取html失败，读取一行数据失败");
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					log.warn(e);
				}
			}
		}
		return sb.toString();
		
	}

	public static void getShareholderUrl(String wholePage) {
//		log.debug(wholePage);
		Pattern ptn = Pattern.compile("(?<=shareholderUrl\\s=\\s\")(.+)(?=\";)");
		Matcher matcher = ptn.matcher(wholePage);
		log.debug( wholePage.indexOf("shareholderUrl") );
		if (matcher.find()) {
			log.debug(matcher.group(0));
		}
		
	}
	

	public static void getShareholderType(String wholePage) {
		Pattern ptn = Pattern.compile("(?<=shareHolderForAll\"\\>)(.*?)(?=\\<)", Pattern.DOTALL);
		Matcher matcher = ptn.matcher(wholePage);
		while (matcher.find()) {
			log.debug(matcher.group(0).trim());
		}
		
	}
	

	public static void getKeyPersonUrl(String wholePage) {
//		log.debug(wholePage);
		Pattern ptn = Pattern.compile("(?<=keyPersonUrl\\s=\\s\")(.+)(?=\";)");
		Matcher matcher = ptn.matcher(wholePage);
		if (matcher.find()) {
			log.debug(matcher.group(0));
		}
		
	}

}
